package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.UserAirdrop;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserAirdropService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop/airdrop")
@Tag(name = "空投表")
@Slf4j
@AllArgsConstructor
public class UserAirdropController {

    private final UserAirdropService userAirdropService;

    private final BusinessNftService businessNftService;

    private final ShopDetailService shopDetailService;


    // @GetMapping( "/page")
    // @Operation(summary =  "空投分页列表", description = "空投分页列表")
    // public ServerResponseEntity<PageVO<Airdrop>> page(@Valid PageDTO pageDTO, String param, Integer status){
    //     YamiShopUser shopUser = SecurityUtils.getShopUser();
    //     PageVO<Airdrop> page = airdropService.page(pageDTO, param, status, shopUser.getShopId());
    //     return ServerResponseEntity.success(page);
    // }
    //
    //
    // @PostMapping("/execute")
    // @Operation(summary =  "空投立即执行", description = "空投立即执行")
    // public ServerResponseEntity executeImmediately(@RequestParam Long airdropId){
    //     Airdrop byId = airdropService.getById(airdropId);
    //     if (Objects.isNull(byId) || !byId.getStatus().equals(AirdropEnum.IMPORTCOMPLETE.value())) {
    //         log.info(airdropId + "无此空投或该状态不能空投");
    //         throw new YamiShopBindException("无此空投或该状态不能空投");
    //     }
    //     airdropRecordService.startAirdrop(byId);
    //     byId.setStatus(AirdropEnum.INAIRDROP.value());
    //     byId.setUpdateTime(new Date());
    //     airdropService.updateById(byId);
    //     return ServerResponseEntity.success("空投执行中…");
    // }
    //
    //
    // @GetMapping( "/cancel/{airdropId}")
    // @Operation(summary =  "取消空投", description = "取消空投")
    // public ServerResponseEntity cancel(@PathVariable("airdropId") Long airdropId){
    //     airdropService.cancelAirdrop(airdropId);
    //     return ServerResponseEntity.success();
    // }
    //
    //
    // @PostMapping( "/importExcel")
    // @ResponseBody
    // @Operation(summary =  "导入空投名单", description = "导入空投名单")
    // public ServerResponseEntity importExcel(@RequestParam("excelFile") MultipartFile excelFile) {
    //     YamiShopUser shopUser = SecurityUtils.getShopUser();
    //     if (Objects.isNull(excelFile)) {
    //         // 网络繁忙，请稍后重试
    //         throw new YamiShopBindException("网络繁忙，请稍后重试");
    //     }
    //     airdropService.parseFile(excelFile, shopUser.getShopId());
    //     return ServerResponseEntity.success("空投名单导入中…");
    // }
    //
    //
    // @GetMapping( "/downloadModel")
    // @Operation(summary =  "下载空投导入模板", description = "下载空投导入模板")
    // public void downloadModel(HttpServletResponse response) {
    //     airdropService.exportModel(response);
    // }
    //
    // @Autowired
    // private BusinessNftService businessNftService;
    //
    @PutMapping("/sendUserDrop")
    @Operation(summary = "店铺批量空投艺术品或盲盒给会员")
    public ServerResponseEntity<Boolean> sendUserDrop(@RequestBody UserUpdateParam param) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
        List<String> userIds = param.getUserIds();
        if (CollectionUtils.isEmpty(userIds)) {
            return ServerResponseEntity.success();
        }
        List<SendUserDropsParam> drops = param.getDrops();
        if (CollectionUtils.isEmpty(drops)) {
            return ServerResponseEntity.success();
        }
        param.setBusinessType(2);
        businessNftService.bindDropById(param, shopDetail);
        return ServerResponseEntity.success();
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取用户空投列表信息", description = "分页获取用户空投列表信息")
    public ServerResponseEntity<IPage<UserAirdrop>> page(@ParameterObject UserAirdrop userAirdrop, PageParam<UserAirdrop> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        userAirdrop.setDropShopId(shopId);
        IPage<UserAirdrop> userDropPage = userAirdropService.getUserAirdropPage(page, userAirdrop);
        return ServerResponseEntity.success(userDropPage);
    }


}
